package com.seek.inter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seek.cache.CacheUtil;
import com.seek.common.handler.SpringContextHolder;
import com.seek.inter.interdto.InterDtoUser;
import com.seek.model.dto.ResultInfo;
import com.seek.model.entity.TSysRole;
import com.seek.model.entity.TSysUser;
import com.seek.service.ReadService;
import com.seek.service.SysRoleService;
import com.seek.service.SysUserService;
import com.seek.util.CommonUtil;
import com.seek.util.JsonUtil;
import com.seek.util.SmsCodeCacheUtil;
import com.seek.util.UUIDUtil;
import com.seek.util.WebUtil;

public class UserInter {
	private static Logger logger = LoggerFactory.getLogger(UserInter.class);
	private SysUserService sysUserService = (SysUserService)SpringContextHolder.getBean("sysUserService");
	private SysRoleService sysRoleService = (SysRoleService)SpringContextHolder.getBean("sysRoleService");
	private ReadService readService = (ReadService)SpringContextHolder.getBean("readService");
	
	/**
	 * 用户登录接口  正常登录返回用户信息，否则返回错误信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ResultInfo login(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		String loginName = reqJson.getString("loginName");
		String password = reqJson.getString("password");
		if(companyId == null || companyId<1 || StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
			result.setStatus(1);
			result.setMessage("用户名或密码为空！");
			return result;
		} else {
			// 取得用户的基本资料
			StringBuffer sb = new StringBuffer("select a.id userId, a.user_name userName, (case when a.agent_id>0 then '代理商' else '商户' end) userType,a.is_used isUsed,");
			sb.append(" b.name companName, b.logo companyLogo, a.phone_no phoneNo, a.last_login_time lastLoignTime,a.role_id roleId,c.role_name roleName,")
			.append(" c.is_admin isAdmin,(case when a.agent_id>0 then 1 else 0 end) isAgent, a.is_auditor isAuditor, a.is_overduer isOverduer,a.hide_phone hidePhone,")
			.append(" a.agent_id agentId,(select c.fid from t_company_agent c where c.id=a.agent_id) fid")
			.append(" from t_sys_user a, t_company b,t_sys_role c")
			.append(" where a.is_used=1 and c.is_used=1 and a.company_id=b.id and a.role_id=c.id")
			.append(" and a.company_id=? and a.login_name=? and a.password=?");
			Map<String,Object> ret = readService.findOneMapBySql(sb.toString(), new Object[]{companyId,loginName,password});
			if (ret == null || ret.get("userId") == null){
				result.setStatus(1);
				result.setMessage("用户名或密码错误！！");
				return result;
			}
			
			int isAdmin = CommonUtil.getIntValue(ret.get("isAdmin"));
			// 判断当前用户是否为管理员，如果是则还需要获取统计数据
			if (isAdmin>0){
				Map<String,Object> m = JsonUtil.fromJson(CacheUtil.getRedisData("TODAYCOUNT_CACHE_"+companyId));
				if (m != null) ret.putAll(m);
				
				ret.put("sum1", JSONArray.parseArray(CacheUtil.getRedisData("OPER_STATS_USER_TREND"), Map.class));
				ret.put("sum2", JSONArray.parseArray(CacheUtil.getRedisData("OPER_STATS_LOAN_TREND"), Map.class));
				ret.put("sum3", JSONArray.parseArray(CacheUtil.getRedisData("OPER_STATS_BAD_AGE"), Map.class));
				ret.put("sum4", JSONArray.parseArray(CacheUtil.getRedisData("OPER_STATS_BAD_CITY"), Map.class));	
			}
			
			// 修改用户的最近登录IP和最近登录时间
			String ip = WebUtil.getIpAddr(request);
			String sql = "update t_sys_user set last_login_time=now(),authed_ip=? where id="+ret.get("userId");
			sysUserService.executeUpdate(sql, new Object[]{ip});
		
			InterDtoUser en = new InterDtoUser();
			en.setCompanyId(""+companyId);
			en.setId(""+ret.get("userId"));
			en.setIsAdmin(isAdmin);
			en.setIsAudit(CommonUtil.getIntValue(ret.get("isAuditor")));
			en.setIsOverduer(CommonUtil.getIntValue(ret.get("isOverduer")));
			en.setPhoneNo(""+ret.get("phoneNo"));
			en.setHidePhone(CommonUtil.getIntValue(ret.get("hidePhone")));
			en.setIsUsed(CommonUtil.getIntValue(ret.get("isUsed")));

			// 生成token
			String token = UUIDUtil.getUUID();
			// 根据token缓存用户资料
			CacheUtil.setLoginCache(token, en);
			String oldToken = CacheUtil.getRedisData("SYSUSER_TOKEN_"+en.getId());
			if (!CommonUtil.isEmpty(oldToken))
				CacheUtil.delRedisData(oldToken);
			CacheUtil.putRedisData("SYSUSER_TOKEN_"+en.getId(), token);
			ret.put("token", token);

			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(ret);
			
			return result;
		}
	}
	
	/**
	 * 通过验证码重置密码
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo resetPassword(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		String phoneNo = reqJson.getString("phoneNo");
		String verificationCode = reqJson.getString("verificationCode");
		String password = reqJson.getString("password");

		// 检查传入的参数
		if (CommonUtil.isEmpty(phoneNo) || CommonUtil.isEmpty(password) || CommonUtil.isEmpty(verificationCode)){
			result.setStatus(1);
			result.setMessage("参数不完整，请检查！"); 
			return result;
		}
		// 检测验证码
		if (!SmsCodeCacheUtil.checkSms(phoneNo, verificationCode)){
			result.setStatus(2);
			result.setMessage("验证码输入错误或已失效！");
			return result;
		}
		// 判断用户是否已存在
		if (!sysUserService.checkUserExists(companyId, phoneNo, null)){
			result.setStatus(3);
			result.setMessage("该手机号对应的用户不存在！");
			return result;
		}

		// 更新客户的密码
		String sql = "update t_sys_user set password=? where company_id=? and phone_no=?";
		sysUserService.executeUpdate(sql, new Object[]{password, companyId, phoneNo});
	
		result.setStatus(0);
		result.setMessage("成功");
		return result;
	}
	
	/**
	 * 在已登录的情况下重置密码 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo modifyPassword(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long uid = reqJson.getLong("userId");
		String oldPassword = reqJson.getString("oldPassword");
		String newPassword = reqJson.getString("newPassword");
		
		if (CommonUtil.isEmpty(oldPassword) || CommonUtil.isEmpty(newPassword) || uid == null || uid<1){
			result.setStatus(1);
			result.setMessage("参数错误！");
			return result;
		}
		oldPassword = oldPassword.trim();
		newPassword = newPassword.trim();
		
		String sql = "update t_sys_user set password=? where id=? and password=?";
		int ret = sysUserService.executeUpdate(sql, new Object[]{newPassword, uid, oldPassword});
		if (ret < 1){
			result.setStatus(1);
			result.setMessage("密码更新失败，您的旧密码输入错误！"); 
			return result;
		}
		result.setStatus(0);
		result.setMessage("成功");
		return result;
	}
	
	/**
	 * 获取角色列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getRoleList(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		String roleName = reqJson.getString("roleName");
		String roleStatus = reqJson.getString("roleStatus");
		
		// 拼接检索条件
		if (!CommonUtil.isEmpty(roleName))
			roleName = "%"+roleName+"%";
		StringBuffer where = new StringBuffer("");
		where.append(" and (? is null or a.role_name like ?)");
		if (!"0".equals(roleStatus) && !"1".equals(roleStatus))
			roleStatus = null;
		where.append(" and (? is null or a.is_used=?)");
		// 取得总记录行数
		String sql = "select count(1) cnt from t_sys_role a where a.company_id=?"+where.toString();
		
		StringBuffer sb = new StringBuffer("select a.id roleId, a.role_name roleName, a.is_admin isAdmin, a.is_agent isAgent,");
		sb.append(" (select count(1) from t_sys_user b where b.role_id=a.id and b.is_used=1) userNum,")
		.append(" a.remark,a.is_used roleStatus, a.create_time createTime, a.modify_time modifyTime")
		.append(" from t_sys_role a where a.company_id=? ");
		sb.append(where).append(" order by a.id desc");
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.pageQueryBySql(sb.toString(), sql, reqJson.getIntValue("pageSize"), reqJson.getIntValue("pageNo"), new Object[]{companyId,roleName,roleName,roleStatus,roleStatus}));
		return result;
	}
	
	/**
	 * 获取角色详情
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getRoleDetail(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long roleId = reqJson.getLong("roleId");
		if (roleId == null || roleId<1){
			result.setStatus(1);
			result.setMessage("参数错误！");
			return result;
		}
		
		String sql = "select a.id roleId, a.role_name roleName, a.is_admin isAdmin, a.is_agent isAgent,a.remark from t_sys_role a where a.id=? ";
		Map<String, Object> ret = readService.findOneMapBySql(sql, new Object[]{roleId});
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(ret);
		return result;
	}
	
	/**
	 * 保存角色
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo saveRole(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		try{
			TSysRole en = sysRoleService.save(reqJson);
			Map<String, Object> ret = new HashMap<String, Object>();
			ret.put("id", en.getId());
			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(ret);
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 获取商户用户列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getUserList(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		String userName = reqJson.getString("userName");
		String roleId = reqJson.getString("roleId");
		String userStatus = reqJson.getString("userStatus");
		
		// 拼接检索条件
		if (!CommonUtil.isEmpty(userName))
			userName = "%"+userName+"%";
		StringBuffer where = new StringBuffer("");
		where.append(" and (? is null or a.user_name like ?)");
		where.append(" and (? is null or a.role_id=?)");
		if (!"0".equals(userStatus) && !"1".equals(userStatus))
			userStatus = null;
		where.append(" and (? is null or a.is_used=?)");
		
		// 取得总记录行数
		String sql = "select count(1) cnt from t_sys_user a, t_sys_role b where a.role_id=b.id and a.company_id=? and a.agent_id is null"+where.toString();

		StringBuffer sb = new StringBuffer("select a.id userId, a.user_name userName, a.login_name loginName,b.role_name roleName,");
		sb.append(" a.phone_no phoneNo, b.is_admin isAdmin, a.is_used userStatus, a.last_login_time lastLoginTime,a.authed_ip authedIp")
		.append(" from t_sys_user a, t_sys_role b where a.role_id=b.id and a.company_id=? and a.agent_id is null");
		sb.append(where).append(" order by a.id desc");
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.pageQueryBySql(sb.toString(), sql, reqJson.getIntValue("pageSize"), reqJson.getIntValue("pageNo"), new Object[]{companyId,userName,userName,roleId,roleId,userStatus,userStatus}));
		return result;
	}
	
	/**
	 * 获取商户下代理商用户列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getAgentUserList(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		String userName = reqJson.getString("userName");
		String roleId = reqJson.getString("roleId");
		String userStatus = reqJson.getString("userStatus");
		String myAgentId = reqJson.getString("myAgentId");
		String agentId = reqJson.getString("agentId");
		
		// 拼接检索条件
		StringBuffer where = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		params.add(companyId);
		if (!CommonUtil.isEmpty(userName)){
			userName = "%"+userName+"%";
			where.append(" and (a.user_name like ?)");
			params.add(userName);
		}
		if (CommonUtil.getLongValue(roleId)>0){
			where.append(" and (a.role_id=?)");
			params.add(roleId);
		}
		if ("0".equals(userStatus) || "1".equals(userStatus)){
			where.append(" and (a.is_used=?)");
			params.add(userStatus);
		}
		if (CommonUtil.getLongValue(myAgentId)>0){
			where.append(" and a.agent_id in (select id from t_company_agent where id=? or fid=?)");
			params.add(myAgentId);
			params.add(myAgentId);
		}
		if (CommonUtil.getLongValue(agentId)>0){
			where.append(" and a.agent_id=?");
			params.add(agentId);
		}
		
		// 取得总记录行数
		String sql = "select count(1) cnt from t_sys_user a, t_sys_role b, t_company_agent c where a.role_id=b.id and a.agent_id=c.id and a.company_id=?"+where.toString();

		StringBuffer sb = new StringBuffer("select a.id userId, a.user_name userName, a.login_name loginName,b.role_name roleName,");
		sb.append(" a.phone_no phoneNo, c.agent_name agentName,a.is_used userStatus, a.last_login_time lastLoginTime,a.authed_ip authedIp")
		.append(" from t_sys_user a, t_sys_role b, t_company_agent c where a.role_id=b.id and a.agent_id=c.id and a.company_id=?");
		sb.append(where).append(" order by a.id desc");
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.pageQueryBySql(sb.toString(), sql, reqJson.getIntValue("pageSize"), reqJson.getIntValue("pageNo"), params.toArray()));
		return result;
	}
	
	/**
	 * 获取角色下拉
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getRoleDdlb(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		int type = CommonUtil.getIntValue(reqJson.getString("type"));
		
		String sql = "select id roleId,role_name roleName from t_sys_role where company_id=? and is_used=1 ";
		if (type == 1){ // 商户角色
			sql += " and is_agent=0";
		}else if (type == 2){// 代理商角色
			sql += " and is_agent=1";
		}
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.findMapBySql(sql, new Object[]{companyId}));
		return result;
	}
	
	/**
	 * 获取商户用户详情
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getUserDetail(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long userId = reqJson.getLong("userId");
		if (userId == null || userId<1){
			result.setStatus(1);
			result.setMessage("参数错误！");
			return result;
		}
		
		String sql = "select a.id userId, a.user_name userName, a.login_name loginName, a.role_id roleId,a.phone_no phoneNo,"
					+"a.is_auditor isAuditor,a.is_overduer isOverduer, a.remark,a.hide_phone hidePhone from t_sys_user a where id=?";
		Map<String, Object> ret = readService.findOneMapBySql(sql, new Object[]{userId});
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(ret);
		return result;
	}
	
	/**
	 * 保存商户用户
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo saveUser(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		try{
			TSysUser en = sysUserService.save(reqJson);
			// 刷新用户缓存
			String token = CacheUtil.getRedisData("SYSUSER_TOKEN_"+en.getId());
			if (!CommonUtil.isEmpty(token)){
				InterDtoUser user = CacheUtil.getLoginCache(token);
				if (user != null){
					user.setHidePhone(CommonUtil.getIntValue(en.getHidePhone()));
					user.setIsAudit(en.getIsAuditor());
					user.setIsOverduer(en.getIsOverduer());
					CacheUtil.putRedisData(token, JsonUtil.toJson(user));
				}
			}
			Map<String, Object> ret = new HashMap<String, Object>();
			ret.put("id", en.getId());
			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(ret);
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 获取代理商下拉
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getAgentDdlb(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		Long myAgentId = reqJson.getLong("myAgentId");
		int type = CommonUtil.getIntValue(reqJson.getString("type"));
		
		String sql = "select a.id agentId,a.agent_name agentName from t_company_agent a where a.is_used=1 and a.company_id=?";
		if (myAgentId != null && myAgentId.longValue()>0){
			sql += " and (a.id="+myAgentId+" or a.fid="+myAgentId+")";
		}
		if (type > 0 && type < 3){
			sql += " and a.money_type="+type;
		}else if (type == 3){
			sql += " and a.fid is null";
		}
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.findMapBySql(sql, new Object[]{companyId}));
		return result;
	}
	
	/**
	 * 获取商户下代理商用户详情
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getAgentUserDetail(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long userId = reqJson.getLong("userId");
		if (userId == null || userId<1){
			result.setStatus(1);
			result.setMessage("参数错误！");
			return result;
		}
		
		String sql = "select a.id userId, a.user_name userName, a.login_name loginName, a.role_id roleId,a.phone_no phoneNo,"
					+"a.agent_id agentId, a.remark from t_sys_user a where id=?";
		Map<String, Object> ret = readService.findOneMapBySql(sql, new Object[]{userId});
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(ret);
		return result;
	}
	
	/**
	 * 保存商户下代理商用户
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo saveAgentUser(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		try{
			TSysUser en = sysUserService.saveAgentUser(reqJson);
			Map<String, Object> ret = new HashMap<String, Object>();
			ret.put("id", en.getId());
			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(ret);
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 修改其它操作员的登录密码 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo resetUserPassword(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long uid = reqJson.getLong("userId");
		String password = reqJson.getString("password");
		
		if ( CommonUtil.isEmpty(password) || uid == null || uid<1){
			result.setStatus(1);
			result.setMessage("参数错误！");
			return result;
		}
		password = password.trim();
		
		String sql = "update t_sys_user set password=? where id=?";
		sysUserService.executeUpdate(sql, new Object[]{password, uid});
		result.setStatus(0);
		result.setMessage("成功");
		return result;
	}
	
	/**
	 * 获取指定用户的下拉
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getUserDdlb(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		int type = CommonUtil.getIntValue(reqJson.getString("type"));
		
		String sql = "select id userId,user_name userName from t_sys_user where company_id=? and is_used=1 ";
		if (type == 1){ // 贷款审核员
			sql += " and is_auditor=1";
		}else if (type == 2){// 催收员
			sql += " and is_overduer=1";
		}
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.findMapBySql(sql, new Object[]{companyId}));
		return result;
	}
	
	/**
	 * 角色启用停用
	 * @param request
	 * @param reqJson
	 * @return
	 * @throws Exception
	 */
	public ResultInfo setRoleStatus(HttpServletRequest request, JSONObject reqJson) throws Exception{
		ResultInfo result = new ResultInfo();
		String roleId = reqJson.getString("roleId");
		int status = CommonUtil.getIntValue(reqJson.getString("status"));
		try{
			if (CommonUtil.isEmpty(roleId)) {
				result.setStatus(1);
				result.setMessage("参数错误，请检查!");
				return result;
			}
			
			String sql = "update t_sys_role set is_used=? where id =?";
			sysUserService.executeUpdate(sql,new Object[]{status,roleId});
			
			// 同时设置用户为失效
			if (status == 0){
				sql = "update t_sys_user set is_used=0 where role_id=?";
				sysUserService.executeUpdate(sql,new Object[]{roleId});
			}
		
			result.setStatus(0);
			result.setMessage("成功");
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 用户启用停用
	 * @param request
	 * @param reqJson
	 * @return
	 * @throws Exception
	 */
	public ResultInfo setUserStatus(HttpServletRequest request, JSONObject reqJson) throws Exception{
		ResultInfo result = new ResultInfo();
		String userId = reqJson.getString("userId");
		int status = CommonUtil.getIntValue(reqJson.getString("status"));
		try{
			if (CommonUtil.isEmpty(userId)) {
				result.setStatus(1);
				result.setMessage("参数错误，请检查!");
				return result;
			}
			// 判断用户对应的角色当前是否启用
			String sql = "select b.is_used from t_sys_user a, t_sys_role b where a.role_id=b.id and a.id=? and b.is_used=1";
			Map<String,Object> data = readService.findOneMapBySql(sql, new Object[]{userId});
			if (data == null || data.keySet().size() < 1){
				result.setStatus(1);
				result.setMessage("用户对应的角色当前未启用!");
				return result;
			}
			
			sql = "update t_sys_user set is_used=? where id =?";
			sysUserService.executeUpdate(sql,new Object[]{status,userId});
		
			// 刷新用户缓存
			String token = CacheUtil.getRedisData("SYSUSER_TOKEN_"+userId);
			if (!CommonUtil.isEmpty(token)){
				InterDtoUser user = CacheUtil.getLoginCache(token);
				if (user != null){
					user.setIsUsed(status);
					CacheUtil.putRedisData(token, JsonUtil.toJson(user));
				}
			}
						
			result.setStatus(0);
			result.setMessage("成功");
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 获取客户当天最新统计数据用于显示在主页
	 * @param request
	 * @param reqJson
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ResultInfo getTodayCount(HttpServletRequest request, JSONObject reqJson) throws Exception{
		ResultInfo result = new ResultInfo();
		String companyId = reqJson.getString("companyId");
		try{
			if (CommonUtil.isEmpty(companyId)) {
				result.setStatus(1);
				result.setMessage("参数错误，请检查!");
				return result;
			}
			
			Map<String,Object> m = JsonUtil.fromJson(CacheUtil.getRedisData("TODAYCOUNT_CACHE_"+companyId));
			if (m == null || m.keySet().size() < 1){
				m = new HashMap<String,Object>();
				m.put("verifyNum", 0);
				m.put("zfbVerifyNum", 0);
				m.put("loanAuditNum", 0);
				m.put("overdueNum", 0);
				m.put("certifyUserToday", 0);
				m.put("loanNumToday", 0);
				m.put("loanMoneyToday", 0);
			}
			
			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(m);
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
}
